<?

$authInfo = extractObjectWithKeyFromArray('authInfo', $_SESSION);

$selectFormId = extractNumberWithKeyFromArray("selectFormId", $_REQUEST, 0);
$updateColumns = extractNumberWithKeyFromArray("updateColumns", $_POST, 0);
$processExport = extractNumberWithKeyFromArray("processExport", $_POST, 0);
$outputBlankSpreadsheet = extractNumberWithKeyFromArray("outputBlankSpreadsheet", $_GET, 0);


if($selectFormId && $updateColumns){

	$selectfields = extractArrayWithKeyFromArray("selectfields", $_POST, array());
	
	// DELETE
	$db->executeQuery("DELETE FROM columnexport WHERE formid='" . $selectFormId . "'");
	// INSERT
	foreach($selectfields as $field){
		$db->executeQuery("INSERT INTO columnexport (formid,columntoexport) VALUES ('$selectFormId','$field')");
	}
}



if($selectFormId){
	
	include_once("lib/CmsFormExport.php");
	$cmsFormExport = new CmsFormExport($selectFormId,&$authInfo,&$db);
	
	if($processExport){
		
		
		$exportto = extractNumberWithKeyFromArray("exportto", $_POST, null);
		switch($exportto){
			
			case 1:
				$ext = 'csv';
				break;
			case 2:
				$ext = 'xml';
				break;
			default:
				$ext = 'csv';
		}		
		
		
		$message = $cmsFormExport->processExport($ext);
		if($message->hasErrors){
		
			//echo $message->getMessageForKey('Content');
			echo $message->messages['Content'][0]['message'];
		
		}
		
		exit;
		
	}elseif($outputBlankSpreadsheet){
	
		$cmsFormExport->outputBlankSpreadsheet();
		exit;
		
	}
	
	
}

$query = "SELECT f.* FROM formdescriptor f,  grouplist g WHERE f.formid > 1 AND f.activemodule='1' AND f.sidebarmodule='1'  AND f.gid = g.gid AND f.gid != '1' AND g.enablemode ='1' ";
$db->executeQuery($query);

?>
<div id="cms_browse_heading" class="cms_browse_heading">
  <table width="100%" border="0" cellspacing="0" cellpadding="0">
    <tr>
      <td valign="top" align="left">
	<h1>Export</h1>
      </td>
      <td align="right" valign="top"></td>
    </tr>
  </table>
</div>
<div id="cms_browse_topdivider" class="cms_browse_divider"></div>

<div>


<form name="select_form" method="get" class="cms_form">
<input type="hidden" name="fileNo" value="<?=$fileNo?>">

<table border="0" width="390">
 <tr>
  <td valign="top" width="80">Module:</td>
  <td>
   <select name="selectFormId" onchange="document.select_form.submit()">
    <option value="0">-- Select module --</option>
<?
	while($row = $db->nextObject()){
?>    <option value="<?=$row->formid?>" <?if($selectFormId==$row->formid) print ' selected="selected"';?>><?=$row->formname?></option>
<?
	}
?> 
   </select>
  </td>
</table> 
  
</form>
<br />
<?
if($selectFormId){
?>
<form name="select_fields" id="select_fields" method="post" class="cms_form">
<input type="hidden" name="selectFormId" value="<?=$selectFormId?>" />
<input type="hidden" name="updateColumns" value="1" />
<table border="0">
 <tr>
  <td valign="top" width="80">Select Fields:</td>
  <td>
	<div id="checkList" class="checkListContainer">
	
	  <div id="checkListLeft" class="leftCheckList">
	  
	   <ul id="unselectedList" class="checkList">
	<?
	   $i = 0;
	   foreach(array_diff($cmsFormExport->availableFields,$cmsFormExport->customFields) as $field){
			$e = $cmsFormExport->formElements->getElementDescriptor($cmsFormExport->formDescription->$field);
	?>     <li><label for="selectfields_<?=$i?>"><input id="selectfields_<?=$i?>" name="selectfields[]" type="checkbox" value="<?=$field?>" onclick="selectCheckBoxListItem(this);"  /><?=str_replace(':','',$e->label)?></label></li>
	<?
		$i++;
	   }
	?>    </ul>
	
	  </div>		
		
	  <div id="checkListRight" class="rightCheckList">
	
	   <ul id="selectedList" class="checkList">
	<?
		foreach($cmsFormExport->customFields as $field){
			$e = $cmsFormExport->formElements->getElementDescriptor($cmsFormExport->formDescription->$field);
	?>     <li><label for="selectfields_<?=$i?>"><input id="selectfields_<?=$i?>" name="selectfields[]" type="checkbox" value="<?=$field?>" onclick="deselectCheckBoxListItem(this);" checked="checked" /><?=str_replace(':','',$e->label)?></label></li>
	<?
			$i++;
		}
	?>     
	   </ul>
			
	  </div>
	  
	</div>		
	<script language="javascript" type="text/javascript">
	initChecklist();
	</script>
  </td>
 </tr>
 <tr>
  <td colspan="2" align="right">
  <script language="javascript" type="text/javascript"> function selectAllElementsInSelect( select ) { for (i=0; i < select.options.length; i++){ select.options[i].selected = true; } return; } function doOnSubmitForElementsOnForm( formName ){ f = document.getElementById( formName ); if (f != null){ for (j = 0; j < f.length; j++ ){ e = f[j]; if (e.onsubmit){ e.onsubmit(); } } } return; } </script>
  <br /><a class="cms_form_submit" href="javascript:doOnSubmitForElementsOnForm('select_fields');document.forms['select_fields'].submit();" ><img src="/cms/images/button_go.gif" /></a>
  </td>
 </tr>
</table>
</form>
<br />
<div id="cms_browse_topdivider" class="cms_browse_divider"></div>
<form name="export_data" method="post" class="cms_form">
<input type="hidden" name="selectFormId" value="<?=$selectFormId?>" />
<input type="hidden" name="processExport" value="1" />
<table border="0" width="390">
 <tr>
  <td valign="top" width="80">Export To:</td>
  <td>
    <input type="radio" name="exportto" value="1" onclick="document.export_data.submit()" /> CSV <br />
    <input type="radio" name="exportto" value="2" onclick="document.export_data.submit()" /> XML <br />
  </td>
 </tr>
</table> 
</form>
<?
}
?>

</div>

